# -*- coding: UTF-8 -*-
# 调用mWDN_lstm，实现一段时间内产能的预测（单变量）

import tensorflow as tf
import numpy as np
import os
import sys
from Models_Train import multi_lstm
from Utils import data_process
import matplotlib as mpl

mpl.use("Agg")
import matplotlib.pyplot as plt

# 预测粒度
PREDICT_UNIT = 5
DATA_ROOT = multi_lstm.DATA_ROOT
DATA_NAME = 'pro_multi_variable'
SHEET_NAME = 'day'
LEN_TEST = 120
FEATURE_NUM = 3
use_normal = True
TIME_STEP = multi_lstm.TIME_STEP

# 获取预测数据
LEN_DATA = data_process.getlen_data(DATA_ROOT, DATA_NAME, sheetname=SHEET_NAME)  # 1000
train_data, test_data = data_process.load_data(DATA_ROOT, DATA_NAME, LEN_DATA, LEN_TEST, sheetname=SHEET_NAME,
                                               feature=FEATURE_NUM, normal=use_normal)
test_x, test_y = data_process.generate_multidata(test_data, TIME_STEP, PREDICT_UNIT)

test_x = test_x[-1:, :, :]
# print test_x.shape
TEST_X = np.array(test_x[:, :, 0]).squeeze()
# print TEST_X

# 预测过程
saver = tf.train.import_meta_graph(
    multi_lstm.BASE_DIR + "/LOG_multi-LSTM_pro_multi_variable/MODEL_pro_multi_variable_LSTM_LOSS.ckpt.meta")
with tf.device('/gpu:0'):
    config = tf.ConfigProto()  # 配置tf.Session的运算方式
    config.gpu_options.allow_growth = True  # 当使用GPU时候，Tensorflow运行自动慢慢达到最大GPU的内存
    config.allow_soft_placement = True  # 如果指定设备不存在，允许TF自动分配设备
    config.log_device_placement = False
    sess = tf.Session(config=config)
    # saver.restore(sess, tf.train.latest_checkpoint(mWDN_lstm.BASE_DIR + "/" + mWDN_lstm.LOG_DIR))
    saver.restore(sess,
                  multi_lstm.BASE_DIR + "/LOG_multi-LSTM_pro_multi_variable/MODEL_pro_multi_variable_LSTM_LOSS.ckpt")
    graph = tf.get_default_graph()  # 加载默认图
    input_x = graph.get_tensor_by_name("Placeholder_1:0")
    input_y = graph.get_tensor_by_name("lstm/fully_connected/BiasAdd:0")

    # 预测
    predict_res = sess.run(input_y, feed_dict={input_x: test_x})
    predict_res = np.array(predict_res).squeeze()
    # print predict_res


# 画图
plt.plot(TEST_X)
plt.plot(np.append(TEST_X, predict_res))
plt.savefig(multi_lstm.BASE_DIR + "/LOG_multi-LSTM_pro_multi_variable/res_predict.png")

# 输出数据返回前端
# print TEST_X
# print np.append(TEST_X, predict_res)
